import Mock from 'mockjs'
function param2Obj(url) {
    const search = url.split('?')[1]
    if (!search) {
        return {}
    }
    return JSON.parse(
        '{"' +
        decodeURIComponent(search)
            .replace(/"/g, '\\"')
            .replace(/&/g, '","')
            .replace(/=/g, '":"') +
        '"}'
    )
}
let List = []
const count = 1
for (let i = 0; i < count; i++) {
    List.push(
        Mock.mock({
            id: Mock.Random.guid(),
            // 'id|+1': 1,
            aname: Mock.Random.cword(2,4)+"餐厅",
            address: Mock.mock('@county(true)'),
            img64Data:''
        })
    )
}
export default {
    getNCardData:() => {
        return {
            code: 200,
            data:{
                
                cards: [
                    // ...List
                    // { id: 1, aname: '客里客气家常菜', address: '闽侯县-软件园F区生活服务中心一层' },
                    // { id: 2, aname: '瑞幸咖啡', address: '闽侯县-软件园F区生活服务中心一层' },
                    // { id: 3, aname: '肯德基', address: '闽侯县-软件园F区生活服务中心一层' },
                    // { id: 4, aname: '库迪咖啡', address: '闽侯县-软件园F区生活服务中心一层' },
                    // { id: 5, aname: '麦当劳', address: '闽侯县-软件园F区生活服务中心一层' },
                    // { id: 6, aname: '客里客气家常菜-6', address: '闽侯县-软件园F区生活服务中心一层' },
                    // { id: 7, aname: '客里客气家常菜-7', address: '闽侯县-软件园F区生活服务中心一层' },
                    // { id: 8, aname: '客里客气家常菜-8', address: '闽侯县-软件园F区生活服务中心一层' },
                    // { id: 9, aname: '客里客气家常菜-9', address: '闽侯县-软件园F区生活服务中心一层' },
                    // { id: 10, aname: '客里客气家常菜-9', address: '闽侯县-软件园F区生活服务中心一层' },
                    
                ],
                message: '获取成功'
            }
            
        };
    },
    /**
     * 获取列表
     * 要带参数 name, page, limt; name可以不填, page,limit有默认值。
     * @param name, page, limit
     * @return {{code: number, count: number, data: *[]}}
     */
    // getUserList: config => {
    //     //limit默认是10，因为分页器默认也是一页10个
    //     const { name, page = 1, limit = 10 } = param2Obj(config.url)

    //     const mockList = List.filter(user => {
    //         //如果name存在会，根据name筛选数据
    //         if (name && user.name.indexOf(name) === -1) return false
    //         return true
    //     })
    //     //分页
    //     const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
    //     return {
    //         code: 200,
    //         data: {
    //             list: pageList,
    //             count: mockList.length, //数据总条数需要返回
    //         }
    //     }
    // },

    /**
     * 删除用户
     * @param id
     * @return {*}
     */
    deleteNCardData: config => {
        console.log("Request URL:", config.url);
        const { id } = param2Obj(config.url)//包裹在get请求中
        // const { id } = config;
        console.log(id);//输出11和undifine
        
        if (!id) {
            return {
                code: -999,
                message: '参数不正确'
            }
        } else {
            List = List.filter(u => u.id !== id)
            return {
                code: 200,
                message: '成功'
            }
        }
    },
    /**
   * 增加用户
   * @param aname, address
   * @return {{code: number, data: {message: string}}}
   */
    addNCardData: config => {
        const { aname, address,img64Data } = JSON.parse(config.body)
        // console.log(aname, address,img64Data);
        
        List.unshift({
            id: Mock.Random.guid(),
            aname: aname,
            address: address,
            img64Data:img64Data,
        })
        return {
            code: 200,
            data: {
                message: '添加成功'
            }
        }
    },
    /**
   * 修改用户
   * @param id, aname, address, age, birth, sex
   * @return {{code: number, data: {message: string}}}
   */
    editNCardData: config => {
        const parsedBody = JSON.parse(config.body);
        const { id, aname, address, img64Data } = parsedBody;        
        List.some(u => {
            if (u.id === id) {
                u.aname = aname
                u.address = address
                u.img64Data = img64Data
                return true
            }
        })
        return {
            code: 200,
            data: {
                message: '编辑成功'
            }
        }
    }
};